    package com.etv.launcher.android.application;

    import android.app.Application;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.SharedPreferences;
    import android.content.res.Configuration;
    import android.content.res.Resources;
    import android.net.ConnectivityManager;

    import androidx.appcompat.app.AppCompatDelegate;

    import com.etv.launcher.android.broadcast.GlobalBroadcastReceiver;
    import com.etv.launcher.common.constance.PreConstance;
    import com.etv.launcher.common.utils.FileUtil;

    import java.io.File;
    import java.util.Locale;

    import javax.inject.Inject;

    import dagger.hilt.android.HiltAndroidApp;

    @HiltAndroidApp
    public class TvApplication  extends Application {
        //共享参数
        private SharedPreferences preferences;

        //封面目录
        public static File ICON_CACHE;

        //漫画页缓存目录
        public static File BANNER_CACHE;

        //壁纸存储地方
        public static File WALLPAPER_CACHE;

        //广播
        @Inject
        GlobalBroadcastReceiver broadcastReceiver;


        @Override
        public void onCreate(){
            super.onCreate();
            preferences = getSharedPreferences(PreConstance.COMIC_PRE, Context.MODE_PRIVATE);

            this.initThemeMode();
            this.initApplicationPath();
            this.initGlobalBroadcastReceiver();
        }


        /**
         * 注册所有的广播
         */
        private void initGlobalBroadcastReceiver(){
            IntentFilter intentFilter = new IntentFilter();

            // 1. 电池状态变化
            intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);

            // 2. 应用安装/卸载
            intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
            intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
            intentFilter.addDataScheme("package");

            // 3. 充电状态
            intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
            intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);

            // 4. 网络状态（需动态注册）
            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

            registerReceiver(broadcastReceiver, intentFilter);
        }

        /**
         * 设置软件明亮模式
         */
        private void initThemeMode(){
            int themeMode = preferences.getInt(PreConstance.THEME_MODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
            //设置主题暗夜模式
            AppCompatDelegate.setDefaultNightMode(themeMode);
        }

        /**
         * 初始化软件目录
         * 封面目录
         * 内容缓存目录
         */
        private void initApplicationPath(){
            //图标
            ICON_CACHE = new File(getApplicationContext().getExternalCacheDir(),"icon");

            //banner
            BANNER_CACHE = new File(getApplicationContext().getExternalCacheDir(),"banner");

            WALLPAPER_CACHE = new File(getApplicationContext().getExternalFilesDir(null),"wallpaper");

            if (!BANNER_CACHE.exists())
                FileUtil.createDirectory(BANNER_CACHE.getPath());

            if (!ICON_CACHE.exists())
                FileUtil.createDirectory(ICON_CACHE.getPath());

            if (!WALLPAPER_CACHE.exists())
                FileUtil.createFile(WALLPAPER_CACHE.getPath());
        }

        /**
         * 更新上下文的语言设置
         */
        private Context updateBaseContextLocale(Context context) {
    //        String languageCode = preferences.getString("language_code", ""); // 假设这是你保存语言代码的键
            Locale locale = Locale.ENGLISH;
            Locale.setDefault(locale);

            Resources resources = context.getResources();
            Configuration configuration = resources.getConfiguration();
            configuration.setLocale(locale);
            context = context.createConfigurationContext(configuration);
            return context;
        }
    }
